package com.bjpowernode.boss.mvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.common.enums.PublicStatus;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.common.exception.BossException;
import com.bjpowernode.boss.common.utils.NoUtil;
import com.bjpowernode.boss.dao.entity.PayProduct;
import com.bjpowernode.boss.dao.mapper.PayProductMapper;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.form.product.PayProductForm;
import com.bjpowernode.boss.mvc.service.PayProductService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class PayProductServiceImpl implements PayProductService {

    @Resource
    private PayProductMapper payProductMapper;

    @Override
    public IPage<PayProduct> pageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<PayProduct> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("edit_time");

        return payProductMapper.selectPage(Page.of(pageNo, pageSize), wrapper);
    }

    @Override
    public void addPayProduct(PayProductForm productForm) {
        // product_code 是唯一的， product_name 唯一的
        PayProduct payProduct = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_code", productForm.getProductCode().trim()));

        if (payProduct != null) {
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_CODE_EXISTS);
        }

        payProduct = payProductMapper.selectOne(new QueryWrapper<PayProduct>()
                .eq("product_name", productForm.getProductName().trim()));

        if (payProduct != null) {
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_NAME_EXISTS);
        }

        //添加新的支付产品
        payProduct = new PayProduct();
        payProduct.setId(NoUtil.getUUID());
        payProduct.setProductCode(productForm.getProductCode().trim());
        payProduct.setProductName(productForm.getProductName().trim());
        payProduct.setEditTime(new Date());
        payProduct.setStatus(PublicStatus.DISABLE.name());
        payProduct.setRemark("创建新的支付产品");
        payProductMapper.insert(payProduct);

    }

    @Override
    public List<DicEntity> queryProductDicList() {
        List<DicEntity> dicEntityList = payProductMapper.selectProductDicList();
        return dicEntityList;
    }
}
